function StoreTest(name) {
	this.base = TestCase;
	this.base(name);
	this.className = "StoreTest";
}
StoreTest.prototype = new TestCase;
StoreTest.testing = function () { 
	return [ "file:///home/dafyddj/src/takecontrol/trunk/components/TakeControlService.js" ]; 
}

StoreTest.prototype.setUp = function () {
	this.date = new Date(2006, 9, 4).getW3CDate();
	this.database = new MockDatabase();
	this.store = new Store(this.date, this.database);
	this.store.active = true;
	this.duration55 = 55;
	this.duration300 = 300;
	this.siteIdGoogle = "www.google.com";
}

StoreTest.prototype.testTriggerDateChange = function () {
	var newDate = new Date(2006, 9, 10).getW3CDate();
	this.store.triggerDateChange(newDate);
	//check that data is saved
	assertEquals("saveCalled", 1, this.database.saveCalled);
	//check that durations have been reset
	assertEquals("totalDuration", 0, this.store.totalDuration);
	assertEquals("siteDuration", 0, this.store.siteDuration);
	//check that date field has been changed
	assertEquals("currentDate", newDate, this.store.currentDate);
}

StoreTest.prototype.testIncrementDefault = function () {
	this.database.siteDuration = this.duration55;
	this.database.dailyTotal = this.duration300;
	this.store._activeSiteId = this.siteIdGoogle;
	
	this.store.increment();
	
	assertEquals("totalDuration", this.duration300 + 1, this.store.totalDuration);
	assertEquals("siteDuration", this.duration55 + 1, this.store.siteDuration);
}

StoreTest.prototype.testIncrementDummy = function () {
	this.database.siteDuration = this.duration55;
	this.database.dailyTotal = this.duration300;
	
	this.store._activeSiteId = Store.dummySiteId;
	this.store.increment();
	
	assertEquals("totalDuration", this.duration300, this.store.totalDuration);
	assertEquals("siteDuration", this.duration55, this.store.siteDuration);
}

StoreTest.prototype.testIncrementWithInterval = function () {
	var interval = 120;
	this.database.siteDuration = this.duration55;
	this.database.dailyTotal = this.duration300;
	this.store._activeSiteId = this.siteIdGoogle;
	
	this.store.increment(interval);

	assertEquals("totalDuration", this.duration300 + interval, this.store.totalDuration);
	assertEquals("siteDuration", this.duration55 + interval, this.store.siteDuration);
}

StoreTest.prototype.testDecrement = function () {
	this.database.siteDuration = this.duration55;
	this.database.dailyTotal = this.duration300;

	this.store.decrement();

	assertEquals("totalDuration", this.duration300 - 1, this.store.totalDuration);
	assertEquals("siteDuration", this.duration55 - 1, this.store.siteDuration);
}

StoreTest.prototype.testChangeSite_http = function () {
	this.store.changeSite(new MockDOMLocation("http:", this.siteIdGoogle));
	assertTrue(this.store.active);
	assertEquals(this.siteIdGoogle, this.store._activeSiteId);
}

StoreTest.prototype.testChangeSite_about = function () {
	this.store._activeSiteId = this.siteIdGoogle;
	this.store.changeSite(new MockDOMLocation("about:"));
	assertFalse(this.store.active);
	assertEquals(Store.dummySiteId, this.store._activeSiteId);
}

StoreTest.prototype.testChangeSite_file = function () {
	var siteId = "/home/dafyddj/work/gtd/d3.html";
	this.store.changeSite(new MockDOMLocation("file:", null, siteId));
	assertTrue(this.store.active);
	assertEquals(siteId, this.store._activeSiteId);
}

StoreTest.prototype.testChangeSite_noLocation = function () {
	this.store._activeSiteId = this.siteIdGoogle;
	this.store.changeSite(undefined);
	assertFalse(this.store.active);
	assertEquals(Store.dummySiteId, this.store._activeSiteId);
}

StoreTest.prototype.testActive_dummySite_userActive = function () {
	this.store.changeSite(undefined);
	this.store.active = true;
	assertFalse(this.store.active);
}

function MockDatabase() {
	this.siteDuration;
	this.dailyTotal;
	this.saveCalled = 0;
}
MockDatabase.prototype.save = function () {
	this.saveCalled++;
}

MockDatabase.prototype.getSessionDuration = function (siteId, date) {
	return this.siteDuration;
}
MockDatabase.prototype.updateSessionDuration = function (siteId, date, duration) {
	this.siteDuration = duration;
}
MockDatabase.prototype.getDailyTotal = function (date) {
	return this.dailyTotal;
}
MockDatabase.prototype.updateDailyTotal = function (date, totalTimeToday) {
	this.dailyTotal = totalTimeToday;
}

function MockDOMLocation(protocol, hostname, pathname) {
	this.protocol = protocol;
	this.hostname = hostname;
	this.pathname = pathname;
}
